package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Employee;
import Database.DatabaseConnector;
import ExceptionClasses.InvalidDateBoundException;

@WebServlet("/ListAllEmployees")
public class ListAllEmployees extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		
			DatabaseConnector dbConnect = new DatabaseConnector();
			HttpServletRequest req = (HttpServletRequest) request;
			HttpServletResponse res = (HttpServletResponse) response;
			HttpSession session = req.getSession();
			String business = (String) session.getAttribute("username");
			ArrayList<Employee> employees;
			JSONObject result = new JSONObject();
			
			try {
				
				employees = dbConnect.getEmployees(business, null, null, null, null, null, null, -1, -1, null, null, null, null, null, null, true);
				
				JSONArray jArray = new JSONArray();
				try {
					for(Employee employee : employees) {
						JSONObject obj = new JSONObject();
						obj.put("employeeid", employee.getEmployeeID());
						obj.put("name", employee.getFirstName() + " " + employee.getLastName());
						jArray.put(obj);
					}
				
					result.put("employees", jArray);
					
				}
				catch (JSONException e) {
				
					e.printStackTrace();
					res.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
				}
			
			
			} catch (InvalidDateBoundException e1) {
				
				e1.printStackTrace();
			}
			
			res.getWriter().print(result.toString());
			res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
			
			
	}
}
